#include "AnimationListener.h"

using namespace Ogre;

AnimationListener::AnimationListener(AnimationState *animationState, OIS::Keyboard *keyboard, Entity *ent, SceneNode* sceneNode)
	: mAnimationState(animationState), mKeyboard(keyboard), mEnt(ent), mSceneNode(sceneNode)
{
}

bool AnimationListener::frameStarted(const FrameEvent &evt)
{
	mKeyboard->capture();
	if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
		return false;

	if(mKeyboard->isKeyDown(OIS::KC_RIGHT))
	{
		mAnimationState = mEnt->getAnimationState("run");
		mAnimationState->setEnabled(true);
		mAnimationState->setLoop(true);
		mSceneNode->translate(Vector3(1.0, 0.0, 0.0));
	}
	else if(mKeyboard->isKeyDown(OIS::KC_LEFT))
	{
		mAnimationState = mEnt->getAnimationState("run");
		mAnimationState->setEnabled(true);
		mAnimationState->setLoop(true);
		mSceneNode->translate(Vector3(-1.0, 0.0, 0.0));
	}
	else
	{
		mAnimationState = mEnt->getAnimationState("idle");
		mAnimationState->setEnabled(true);
		mAnimationState->setLoop(true);
	}


	mAnimationState->addTime(evt.timeSinceLastFrame);

	return true;
}

AnimationListener::~AnimationListener()
{
}
